package Blog;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;


import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;


@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd {

    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8080/blog_detail.html?blogId=",
                "博客详情页", "自动化测试"));
    }

    /*
    * 输入正确的账号密码,登录成功
    * */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    public void LoginSuccess(String username, String password, String url) {

        //打开博客登录页面
        webDriver.get("http://127.0.0.1:8080/blog_login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号zhangsan
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码123456
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的URL
        String cur_url = webDriver.getCurrentUrl();
        //如果URL:http://127.0.0.1:8080/blog_list.html 测试通过,反之不通过---->断言
        Assertions.assertEquals(url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示列表信息
        //用户名是zhangsan测试通过,反之不通过
        String cur_zhangsan = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username,cur_zhangsan);  //前面的值是预期,后面的是测试得到的值
    }

    /*
    * 校验博客列表页
    * */
    @Order(2)
    @Test
    public void BlogList(){
        //打开博客列表
        webDriver.get("http://127.0.0.1:8080/blog_list.html");
        //获取页面上所有的博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素不为0,测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /*
    * 博客详情页
    * URL
    * 博客标题
    * 页面title是"博客详情页"
    * */
    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog_title){
        //找到第一篇博客对应的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();

        //获取当前页面的URL
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();

        //获取当前页面title
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_title = webDriver.getTitle();

        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取博客标题
        String cur_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();

        //校验
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //Assertions.assertEquals(expected_url,cur_url);  //会报错,因为每次的博客URL中的博客ID都不一样
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);
        if(cur_url.contains(expected_url)){
            System.out.println("博客URL测试通过");
        }else{
            System.out.println("测试不通过");
        }

    }

    /*
    * 写博客
    *
    * */
    @Order(3)
    @Test
    public void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //通过Js将内容进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);

        //点击发布
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);

        //获取当前页面URL
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/blog_list.html",cur_url);  //是否跳回列表页
    }

    /*
    * 校验已发布博客标题
    * 校验已发布博客时间
    * */

    @Order(5)
    @Test
    public void BlogInfoChecked() {
        webDriver.get("http://127.0.0.1:8080/blog_list.html");
        //获取第一篇博客标题
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();  //获取第一篇博客title
        //获取第一篇博客发布时间
        String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果是2024-6-5年发布的,测试通过
        if (first_blog_time.contains("2024-06-06")) {
            System.out.println("时间测试通过");
        }else {
            System.out.println("当前时间是: " + first_blog_time);
            System.out.println("测试不通过");
        }
    }

    /*
    * 删除刚才发布的博客
    * 弹窗处理
    * */
    @Order(6)
    @Test
    public void DeleteBlog() throws InterruptedException {
        //打开博客列表页面
        webDriver.get("http://127.0.0.1:8080/blog_list.html");

        //点击查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();

        //点击删除页面
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.operating > button:nth-child(2)")).click();
        sleep(2000);
        webDriver.switchTo().alert().accept();  //弹窗点击确认

        //校验是否删除-->检验博客列表页第一篇题目是不是之前发布的"自动化测试"
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        Assertions.assertNotEquals("自动化测试",first_blog_title);
    }


    /*
    * 注销
    * */
    @Order(7)
    @Test
    void Logout() {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击注销
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();

        //校验URL（登录）
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/blog_login.html",cur_url);
        //校验提交按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }


}
